import api from '@/api'

const Mixin = {
  data() {
    return {
      config: {
        path: {
          form: '/object/form',
          view: '/object/view'
        },

        propForView: {},

        propForList: {
          title: rec => {
            return `${rec.display_name}`
          },
          label: rec => {
            return ``
          },
          value: rec => {
            return ``
          }
        }
      }
    }
  },
  computed: {
    dataDict() {
      return this.$store.state.object.dataDict
    }
  },

  async created() {
    // this.$store.dispatch('user/getInfo')
    const { model } = this.$route.query
    const Model = api.env(model)
    this.model = model
    this.Model = Model
  },

  methods: {
    //

    init_config() {
      const { propForView: propForView_query0 } = this.$route.query

      const propForView_query =
        propForView_query0 && JSON.parse(propForView_query0)

      const Model = this.Model
      const propForList = Model.metadata.propForList || {}
      const propForView = propForView_query || Model.metadata.propForView || {}

      console.log('init_config, ', propForList)

      this.config = {
        ...this.config,
        propForList: { ...this.config.propForList, ...propForList },
        propForView: { ...propForView },
        fields: { ...Model.metadata.fields },
        Model
      }
    },

    get_orderForSearch() {
      const Model = this.Model
      return Model.metadata.order
    },

    get_domainForSearch() {
      const { domain } = this.$route.query
      return domain ? JSON.parse(domain) : {}
    },

    get_fieldsForSearch() {
      //
      const Model = this.Model

      const get_fields = () => {
        const fields0 = Model.metadata.fields
        const fields = Object.keys(fields0).reduce((acc, cur) => {
          const fld = fields0[cur]
          if (fld.type === 'binary') {
            //
          } else if (fld.type === 'one2many') {
            //
          } else {
            acc[cur] = null
          }
          return acc
        }, {})

        return fields
      }

      return Model.metadata.fieldsForSearch || get_fields()
    },

    get_filterForSearch() {
      //
      const Model = this.Model
      // console.log('get_filterForSearch, ', Model.metadata.filter)

      return Model.metadata.filter || []
    },

    get_fieldsForBrowse() {
      const Model = this.Model

      const get_fields = () => {
        const fields0 = Model.metadata.fields

        const fields = Object.keys(fields0).reduce((acc, cur) => {
          const fld = fields0[cur]
          if (fld.type === 'binary') {
            //
            // } else if (fld.type === 'one2many') {
            //
          } else {
            acc[cur] = null
          }
          return acc
        }, {})

        return fields
      }
      return Model.metadata.fieldsForBrowse || get_fields()
    }
  }
}

export default Mixin
